package py.com.fms.com.session.transactions;

//External classes imports 
//Java Classes
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

//Seam Classes
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.international.StatusMessage.Severity;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

import com.ibatis.sqlmap.client.SqlMapClient;

import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;

//project classes imports
import py.com.fms.com.bean.SupplyCostStockBean;
import py.com.fms.com.persistence.services.SupplyCostStockServices;
import py.com.fms.exception.CommonException;
import py.com.fms.fms.bean.UsersBean;
import py.com.fms.fms.persistence.services.UsersServices;
import py.com.fms.session.ComAssistant;
import py.com.fms.session.ServiceSqlMap;
import py.com.fms.session.WithinHolder;
import py.com.fms.utilities.ConvertAssistant;


@SuppressWarnings("unused")
@Name("trnSupplyCostStock")
@Scope(ScopeType.SESSION)
public class TrnSupplyCostStock {
	
	private FacesMessages facesMessages;
	@Logger
	private Log log;
	@In("#{serviceSqlMap}")
	private ServiceSqlMap sqlMap;
	private String initVar;
	private Date today = null;

	// Task Attendees Attributes 
	@In("#{withinHolder}")
	private WithinHolder aid;
	@In("#{convertAssist}")
	private ConvertAssistant convert;
	@In("#{comAssist}")
	private ComAssistant comAssist;
	@In Credentials credentials;
	@In Identity identity;
	private UsersBean user;
	
	// Attribute used for saving and deleting
	private SupplyCostStockBean supplyCostStock;
	private SupplyCostStockBean editSupplyCostStock;
	
	// Grid
	private Collection<SupplyCostStockBean> allSupplyCostStock;
	
	// Services
	private SupplyCostStockServices scSrv;
	private UsersServices usSrv;
	

	/**
	 * [[[[[[[[[[[[ Methods ]]]]]]]]]]]] 
	 * [[[[[[[[[[-------------]]]]]]]]]]
	 * @throws CommonException 
	 */

	/**
	 * This method is used for class initializing 
	 */
	public void initData() throws CommonException {

		System.out.println("Initialization Flag = " + this.getInitVar());

		if ("S".equals(this.getInitVar())) {
			this.initBeans();
			this.initServices();
			this.initAssistants();
			this.initCollections();
			this.loadDataGrid();
			this.loadUserData();

			
		}
		this.setInitVar(null);
		System.out.println("OUT");
				
	}
	
	
	/**
	 * Retrieve the data of the logged user 
	 * and store the data into a user object
	 * 
	 * @return      is a void method, just store the data into an object
	 */
	private void loadUserData() throws CommonException{
		if(credentials != null){
			System.out.println("Credenciales Encontradas");
			this.getUser().setUserName(credentials.getUsername());
			System.out.println("User Name: " + this.getUser().getUserName());
			user = usSrv.getUserDataByName(this.getUser().getUserName());
			this.setUser(user);
			System.out.println("User: " + this.getUser());
		}else{	
			System.out.println("Credenciales Nulas");
		}
	}
	
	
	/**
	 * Initialize the entity Beans 
	 */
	public void initBeans(){
		
        if (supplyCostStock == null){
        	supplyCostStock = new SupplyCostStockBean();
		}
		
		if (editSupplyCostStock == null){
			editSupplyCostStock = new SupplyCostStockBean();
		}
		
		if (user == null){
			user = new UsersBean();
		}	
			
	}
	
	/**
	 * Initialize the services objects 
	 */
	public void initServices() throws CommonException{
		
		if (scSrv == null){
			scSrv = new SupplyCostStockServices(this.sqlMap.getConfig());
		}
			
		if (usSrv == null){
			usSrv = new UsersServices(this.sqlMap.getConfig());
		}		
	}
	
	/**
	 * Initialize the Assistants objects 
	 */
	public void initAssistants(){
		
		if(aid == null){
			aid = new WithinHolder();
		}
		
		if(convert == null){
			convert = new ConvertAssistant();
		}
		
		if(comAssist == null){
			comAssist = new ComAssistant();
		}

	}
	
	/**
	 * Initialize collection objects 
	 */
	public void initCollections(){
		
		if(allSupplyCostStock == null){
			allSupplyCostStock = new ArrayList<SupplyCostStockBean>();
		}

	}
	
	/**
	 * Clean all objects
	 */
	public void cleanData(){

		supplyCostStock = null;
		editSupplyCostStock = null;
		user = null;
		
		scSrv = null;
		usSrv = null;
		
		aid = null;
		convert = null;
		comAssist = null;
        
		allSupplyCostStock = null;

	}
	

	/**
	 * Retrieve all bean data and store it into a collection.
	 * Used to load a Grid in the Visual Layer
 	 */
	private void loadDataGrid() {
		try {
			this.setAllSupplyCostStock(scSrv.getAllSupplyCostStock());
			for (SupplyCostStockBean bean : this.getAllSupplyCostStock()) {
				System.out.println(bean.getSupply());
			}
		} catch (CommonException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Cancel Current operation and clean form objects.
 	 */
	public String cancelData() throws CommonException {
//		this.facesMessages.clear();
		this.setInitVar("S");
		this.cleanData();
		this.initData();
		return null;
	}

	
	public void saveNewData(){
			
		try{
			SupplyCostStockBean newSupplyCostStock = new SupplyCostStockBean();
			
			if(this.getSupplyCostStock() == null){
				System.out.println("Null Entity");
				
			}else{	
				newSupplyCostStock.setSupply(this.getSupplyCostStock().getSupply());
				newSupplyCostStock.setIdBranch(this.getSupplyCostStock().getIdBranch());
				newSupplyCostStock.setIdMeasureUnit(this.getSupplyCostStock().getIdMeasureUnit());
				newSupplyCostStock.setStock(this.getSupplyCostStock().getStock());
				newSupplyCostStock.setRealStock(this.getSupplyCostStock().getRealStock());
				newSupplyCostStock.setMinStock(this.getSupplyCostStock().getMinStock());
				newSupplyCostStock.setBasicStock(this.getSupplyCostStock().getBasicStock());
				newSupplyCostStock.setIdCurrency(this.getSupplyCostStock().getIdCurrency());
				newSupplyCostStock.setNetCost(this.getSupplyCostStock().getNetCost());
				newSupplyCostStock.setGrossCost(this.getSupplyCostStock().getGrossCost());
				newSupplyCostStock.setAverageCost(this.getSupplyCostStock().getAverageCost());
				
				this.scSrv.saveNewSupplyCostStock(newSupplyCostStock);
			}
			
		} catch (CommonException e) {
			// TODO: handle exception
	//		this.facesMessages.add(Severity.ERROR, e.getMessage());
			System.out.println("NO SE PUDO INSERTAR. " + e.getMessage());
			
			log.error(e);
		}finally{
			this.cleanData(); // Cleaning Objects
			this.setInitVar("S"); // Setting initialization Flag			
		}
	}
	
	public void dataToUpd(Long id){
		for(SupplyCostStockBean bean : this.getAllSupplyCostStock()){
			if(bean.getId().equals(id)){
				this.setEditSupplyCostStock(bean);
				
			}
		}
	}
	
	public void updateSupplyCostStock(){
		try{
			
			this.scSrv.updateSupplyCostStock(this.getEditSupplyCostStock());
			
		}catch (CommonException e) {
			// TODO: handle exception
			//		this.facesMessages.add(Severity.ERROR, e.getMessage());
					System.out.println("NO SE PUDO ACTUALIZAR. " + e.getMessage());
					
					log.error(e);
		}
	}
	
	// ACCESSORS AND MUTATORS

	public FacesMessages getFacesMessages() {
		return facesMessages;
	}


	public void setFacesMessages(FacesMessages facesMessages) {
		this.facesMessages = facesMessages;
	}


	public Log getLog() {
		return log;
	}


	public void setLog(Log log) {
		this.log = log;
	}


	public ServiceSqlMap getSqlMap() {
		return sqlMap;
	}


	public void setSqlMap(ServiceSqlMap sqlMap) {
		this.sqlMap = sqlMap;
	}


	public String getInitVar() {
		return initVar;
	}


	public void setInitVar(String initVar) {
		this.initVar = initVar;
	}


	public WithinHolder getAid() {
		return aid;
	}


	public void setAid(WithinHolder aid) {
		this.aid = aid;
	}


	public UsersBean getUser() {
		return user;
	}


	public void setUser(UsersBean user) {
		this.user = user;
	}


	public SupplyCostStockBean getSupplyCostStock() {
		return supplyCostStock;
	}


	public void setSupplyCostStock(SupplyCostStockBean supplyCostStock) {
		this.supplyCostStock = supplyCostStock;
	}


	public SupplyCostStockBean getEditSupplyCostStock() {
		return editSupplyCostStock;
	}


	public void setEditSupplyCostStock(SupplyCostStockBean editSupplyCostStock) {
		this.editSupplyCostStock = editSupplyCostStock;
	}


	public Collection<SupplyCostStockBean> getAllSupplyCostStock() {
		return allSupplyCostStock;
	}


	public void setAllSupplyCostStock(
			Collection<SupplyCostStockBean> allSupplyCostStock) {
		this.allSupplyCostStock = allSupplyCostStock;
	}
	
	
	
	

}
